/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.rapid;

import java.util.ArrayList;
import java.util.HashMap;

import org.anderground.core.drawer.Calculator.Mode;
import org.anderground.core.graphics.PointF;
import org.anderground.core.model.Hub;
import org.anderground.core.model.ModelData;
import org.anderground.core.model.Station;
import org.anderground.core.model.Transfer;
import org.anderground.core.model.Transition;
import org.anderground.core.model.Tunnel;
import org.anderground.modules.drawer.rapid.Temp.Component;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
class Structure {

	private HashMap<Station, Node> nodesMap;

	private HashMap<Transition, Link> linksMap;

	private ModelData modelData;

	private Mode mode;

	public Structure(ModelData modelData, Mode mode) {
		this.modelData = modelData;
		this.mode = mode;
		nodesMap = new HashMap<Station, Node>(modelData.getStations().size(), 1);
		linksMap = new HashMap<Transition, Link>(
				modelData.getTransfers().size() + modelData.getTunnels().size(),
				1);

		initNodes();

		initLinks();

	}

	private void initNodes() {
		for (Hub hub : modelData.getHubs()) {
			ArrayList<Component> components = Temp.Knot.untie(hub, mode);
			for (Temp.Component component : components) {
				Node node = new Node(component.getStations(), component
						.getCoordF());
				for (Station station : component.getStations()) {
					nodesMap.put(station, node);
				}
			}
		}

		for (Station station : modelData.getStations()) {
			if (station.getHub() == null) {
				nodesMap.put(station, new Node(station, new PointF(station,
						mode)));
			}
		}

	}

	private void initLinks() {
		for (Tunnel tunnel : modelData.getTunnels()) {
			Node fromNode = nodesMap.get(tunnel.getFromStation());
			Node toNode = nodesMap.get(tunnel.getToStation());
			Link link = fromNode.searchNeighbor(toNode);
			link.addTransition(tunnel);
			linksMap.put(tunnel, link);
		}

		for (Transfer transfer : modelData.getTransfers()) {
			Node fromNode = nodesMap.get(transfer.getFromStation());
			Node toNode = nodesMap.get(transfer.getToStation());
			Link link = fromNode.searchNeighbor(toNode);
			link.addTransition(transfer);
			linksMap.put(transfer, link);
		}

	}

	public boolean process(Station station) {
		Node node = nodesMap.get(station);
		if (node == null) {
			nodesMap.put(station, new Node(station, new PointF(station, mode)));
		} else {
			Hub hub = station.getHub();
			if (hub == null) {
				node.getCoordF().set(station, mode);
			} else {
				// can not do anything..
				return false;
			}
		}
		return true;
	}

	public HashMap<Station, Node> getNodesMap() {
		return nodesMap;
	}

	public HashMap<Transition, Link> getLinksMap() {
		return linksMap;
	}

}
